package com.navinfo.tripanalysis.common.arithmetic;

import com.navinfo.tripanalysis.common.arithmetic.annotation.Arithmetic;
import com.navinfo.tripanalysis.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.tripanalysis.common.arithmetic.common.CommonData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarVehicleLoadType;
import com.navinfo.tripanalysis.common.arithmetic.enums.LoadTypeEnum;
import com.navinfo.tripanalysis.common.arithmetic.common.VehicleInfo;
import com.navinfo.tripanalysis.common.util.ArithmeticUtils;

import java.awt.geom.Arc2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Optional;

/**
 * 东风新增载荷算法<br/>
 * 依赖{@link VehicleInfo}的发动机输出最大扭距、满半空阀值<br/>
 * 输出：统计(amt载重、vecu载重、载重类型)<br/>
 * @author web
 */
public class VehicleLoadType_DF extends AbstractArithmetic<VarVehicleLoadType> {
    /**
     * 重量状态变化阀值，单位吨
     */
    private final int loadThreshold = 3;

    @Override
    public void calculate() {
        logger.debug("===============东风载荷算法开始===========");
        CommonData commonData = getCommonData();
        Integer amtLoadNew = Optional.ofNullable(commonData.getAmtLoad()).orElse(0);
        Integer vecuLoadNew = Optional.ofNullable(commonData.getVecuLoad()).orElse(0);

        //无效值不参与计算,过滤0和大于300T的数据
        if (amtLoadNew == 0 && vecuLoadNew == 0 || (amtLoadNew > 300000 || vecuLoadNew > 300000) ){
            return;
        }

        VarVehicleLoadType var = getObj();

        int loadValue1;
        if (vecuLoadNew !=null && vecuLoadNew !=0){
            loadValue1 = vecuLoadNew;
        }else {
            loadValue1 = Optional.ofNullable(amtLoadNew).orElse(0);
        }

        var.getLoadList().add(loadValue1);
        var.getLoadSet().add(loadValue1);

        // 与上个值相比的变化范围是否超过阈值
        if ( null == var.getAmtLoad() || Math.abs(amtLoadNew - var.getAmtLoad()) >= loadThreshold*1000){
            var.setAmtLoad(amtLoadNew);
        }
        if ( null == var.getVecuLoad() || Math.abs(vecuLoadNew - var.getVecuLoad()) >= loadThreshold*1000){
            var.setVecuLoad(vecuLoadNew);
        }
//        int loadValue;
//        if (var.getVecuLoad()!=null && var.getVecuLoad()!=0){
//            loadValue = var.getVecuLoad();
//        }else {
//            loadValue = Optional.ofNullable(var.getAmtLoad()).orElse(0);
//        }



        logger.debug("===============东风载荷算法结束===========");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarVehicleLoadType var = getObj();
        statisticDataOut.setAmtLoad(var.getAmtLoad());
        statisticDataOut.setVecuLoad(var.getVecuLoad());

        /**
         * 载荷状态计算
         *      如果车型不在列表内则载荷状态显示为-1
         *      如果车辆VECU和AMT均上报载重以VECU为准
         *   车型载重状态分四段计算
         *   例：
         *   车 || 空载   ||  半载     ||    满载    ||  超载
         *   a  ||＜22.5  || 22.5-50   ||   50-60    ||  ＞60
         */
        LoadTypeEnum loadTypeEnum = LoadTypeEnum.UNDEF;
        VehicleInfo vehicleInfo = drivingData.getVehicleInfo();
        if (vehicleInfo != null) {
            Integer empty = vehicleInfo.getWloadEmpty();
            Integer half = vehicleInfo.getWloadHalf();
            Integer full = vehicleInfo.getWloadFull();
            String carType = vehicleInfo.getCarType();
            String maxLoad = vehicleInfo.getMaxLoad();
            logger.debug("===============东风载荷算法车辆{}，对应的满半空阀值{}，{}，{}===========", terminalId, empty, half, full);
            if(empty!=null && half!=null && full!=null){
                //最终计算使用的在种植
                int loadValue;
                if (var.getVecuLoad()!=null && var.getVecuLoad()!=0){
                    loadValue = var.getVecuLoad();
                }else {
                    loadValue = Optional.ofNullable(var.getAmtLoad()).orElse(0);
                }

                if(loadValue>0) {
                    if (loadValue <= empty*100) {
                        loadTypeEnum = LoadTypeEnum.EMPTY;
                    } else if (loadValue <= half*100) {
                        loadTypeEnum = LoadTypeEnum.HALF;
                    } else if (loadValue <= full*100) {
                        loadTypeEnum = LoadTypeEnum.FULL;
                    } else {
                        loadTypeEnum = LoadTypeEnum.OVER;
                    }
                }
            }else if (carType != null){

                    //最终计算使用的载重值
                    int loadValue;
                    if (var.getVecuLoad()!=null && var.getVecuLoad()!=0){
                        loadValue = var.getVecuLoad();
                    }else {
                        loadValue = Optional.ofNullable(var.getAmtLoad()).orElse(0);
                    }
                    if (loadValue>0){
                        try {
                            if (Double.parseDouble(Optional.ofNullable(maxLoad).orElse("10000") )< loadValue ){
                                loadTypeEnum = LoadTypeEnum.OVER;
                            }
                        }catch (Exception e){
                            logger.debug("===============东风载荷算法车辆{}，最大荷载非法值{}===========", terminalId, maxLoad);
                        }

                }
            }
        }
        //单个驾驶周期内行程大于30分钟,不同质量点数据大于20，车辆载重的标准差/均值小于15%是有效值；否则为无效
        Double  avgload =  ArithmeticUtils.mean(var.getLoadList());
        Double  squtLoad = ArithmeticUtils.standardDeviaction(var.getLoadList());
        Integer loadNum = var.getLoadSet().size();
//statisticData
        if (loadNum >20 && squtLoad/avgload<0.15 ){
            statisticDataOut.setLoadType(loadTypeEnum.getValue());
        }else{
            statisticDataOut.setLoadType(LoadTypeEnum.UNDEF.getValue());
        }

    }

}
